package score;

import beans.GroupScoreCell;
import beans.ScoreGroupContainer;
import beans.SessionManagedBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import lecture.Lecture;
import task.ExerciseSheet;
import user.ScoreGroup;
import user.User;

/**
 *
 * @author Thorsten Ludwig
 */

@ManagedBean(name="enterScoreBean")
@CustomScoped(value="#{window}")
public class EnterScoreManagedBean {
    
    protected ExerciseSheet choosenSheet;
    
    protected List<UserScoreContainer> userScores;
    
    private List<ScoreGroupContainer> sg;
    
    public EnterScoreManagedBean() {

    }

    public ExerciseSheet getChoosenSheet() {
        return choosenSheet;
    }

    public void setChoosenSheet(ExerciseSheet choosenSheet) {
        this.choosenSheet = choosenSheet;
        FacesContext c = FacesContext.getCurrentInstance();
        SessionManagedBean sb = c.getApplication().evaluateExpressionGet(c, "#{sessionBean}", SessionManagedBean.class);
        Lecture lecture = sb.getLectureBean().getLecture();
        this.userScores = this.createCells(choosenSheet, lecture.getStudents());
    }
    
    public List<SelectItem> getItems() {
        List<SelectItem> list = new ArrayList<SelectItem>();
        list.add(new SelectItem(Status.EDITED, "Bearbeitet"));
        list.add(new SelectItem(Status.EXCUSED, "Entschuldigt"));
        list.add(new SelectItem(Status.NOT_EDITED, "Nicht bearbeitet"));
        list.add(new SelectItem(Status.NOT_INSERTED, "Nicht eingetragen"));        
        return list;
    }
    
    public List<ScoreGroupContainer> getScoreGroups() {
        if (sg == null)
            this.initSg();
        return sg;
    }
    
    public void enterGroupsScore() {
        for (ScoreGroupContainer sgc: this.sg){
            for (GroupScoreCell cell: sgc.getScores()) {
                if (cell.isScoreChanged()) {
                    for (int i: cell.getRows()) {
                        UserScoreContainer con = this.userScores.get(i);
                        ScoreCell scoreCell = con.getCells().get(cell.getExercise());
                        scoreCell.getScore().setScore(cell.getScore());
                        scoreCell.getScore().setStatus(cell.getStatus());
                    }
                }
            }
        }
    }
    
    private void initSg() {
        FacesContext c = FacesContext.getCurrentInstance();
        SessionManagedBean sb = c.getApplication().evaluateExpressionGet(c, "#{sessionBean}", SessionManagedBean.class);
        Lecture lecture = sb.getLectureBean().getLecture();
        List<ScoreGroupContainer> sg = new ArrayList<ScoreGroupContainer>();
        
        for (ScoreGroup s: sb.getUser().getScoreGroups()) {
            if (s.getLecture() == lecture || s.getCategorys().contains(this.choosenSheet.getCategory()))
                sg.add(new ScoreGroupContainer(s, this.choosenSheet,lecture.getStudents()));
        }
        this.sg = sg;
    }
    
    protected List<UserScoreContainer> createCells(ExerciseSheet sheet, List<User> user) {
        System.out.println("Zellen erstellt");
        FacesContext c = FacesContext.getCurrentInstance();
        SessionManagedBean sb = c.getApplication().evaluateExpressionGet(c, "#{sessionBean}", SessionManagedBean.class);
        Lecture lecture = sb.getLectureBean().getLecture();
        List<Entry<String,List<Score>>> scores = lecture.getScoreBySheet(sheet);
        List<UserScoreContainer> list = new ArrayList<UserScoreContainer>();
        for (User u: user) {
            List<Score> scoreList = this.getScoreList(scores, u.getUsername());
            List<ScoreCell> cells = new ArrayList<ScoreCell>();
            for (Score s: scoreList) {
                cells.add(new ScoreCell(s));
            }
            
            list.add(new UserScoreContainer(u.getUsername(), cells));
        }
        return list;
    }

    private List<Score> getScoreList(List<Entry<String, List<Score>>> scores, String username) {
        for (Entry<String,List<Score>> e: scores) {
            if (e.getKey().equals(username))
                return e.getValue();
        }
        return null;
    }

    public List<UserScoreContainer> getUserScores() {
        return userScores;
    }
}
